﻿using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Threading;
using System.Reflection;

using mfRCF.Core;
using mfRCF.Core.Extensions;

namespace mfLogger.NetworkAppenders
{
    public class mfHttpAppender : mfRcfCommand, ILoggerAppender, IDisposable
    {
        #region CTOR

        public mfHttpAppender(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
            Name = GetType().Name;
            IsEnabled = true;
            Level = mfLogger.Instance.Level;

            base.ContentTypeOut = "text/html";
            mfRcf.Instance.Commands.Add(this);
        }

        #endregion

        #region ILogger

        public bool IsEnabled { get; set; }

        public enumLogMessageLevel Level
        {
            get;
            set;
        }

        public void Log(mfLogMessage msg)
        {
            HandleMessage(msg);
        }

        public void Debug(string text)
        {
            HandleMessage(new mfLogMessage(enumLogMessageLevel.Debug, text));
        }

        public void Info(string text)
        {
            HandleMessage(new mfLogMessage(text));
        }

        public void Warning(string text)
        {
            HandleMessage(new mfLogMessage(enumLogMessageLevel.Warning, text));
        }

        public void Error(Exception ex)
        {
            HandleMessage(new mfLogMessage(ex));
        }

        public void Error(string text)
        {
            HandleMessage(new mfLogMessage(enumLogMessageLevel.Error, text));
        }

        public void HandleMessage(mfLogMessage msg)
        {
            if (msg.Level >= Level)
            {
                //TODO: do http specific stuff here...
            }
        }

        #endregion

        #region mfRcfCommand

        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            if (!IsEnabled)
            {
                throw new mfRcfException(Name + " is not enabled");
            }

            var htmlContent = BuildHtmlMessageResponse();

            //write response
            httpContext.Response.ContentType = ContentTypeOut;
            httpContext.Response.StatusCode = enumHttpStatusCode.OK;
            httpContext.Response.WriteString(htmlContent);

            httpContext.Response.Close();

            Microsoft.SPOT.Debug.Print(GetType() + " executed, " + Microsoft.SPOT.Debug.GC(false) + " bytes free");
        }


        public String BuildHtmlMessageResponse()
        {
            var strContent = new StringBuilder();

            #region auto-reload
            /*
             <html>
            <head>
            <script type="text/javascript"> 
            <!-- 
            setTimeout('location.reload(true);', 30000); 
            --> 
            </script>
            </head>
            <body>
            <h1>08/05/2013 10:47:46</h1>
            <ul>
             
            strContent.AppendLine(@"<html><head>"
                            + "<script type=\"text/javascript\"><!--");
            strContent.AppendLine("setTimeout('location.reload(true);', 30000); ");//have to be in an own line!!!
            strContent.AppendLine("--> </script>"
                            + "</head><body>"
                            + "<h1>" + DateTime.Now + " " + Name + "</h1>"
                            + "<ol>");*/
            #endregion

            strContent.AppendLine(@"<html><body>"
                            + "<h1>" + DateTime.Now + " " + mfLogger.Instance.Name + "</h1>"
                            + "<ol>");

            for (int i = mfLogger.Instance.Messages.Count-1; i >= 0; i++)
            {
                var msg = mfLogger.Instance.Messages[i];
                strContent.AppendLine("<li>" + msg + "</li>");
            }

            strContent.Append(@"</ol>"
                    + "</body>"
                + "</html>");

            return strContent.ToString();
        }
        #endregion

        public void Dispose()
        {
            mfRcf.Instance.Commands.Remove(this);
        }

    }
}
